﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Derbster.Domain;

namespace Derbster.Data.Test
{
    public static class TestHelper
    {
        public static string[] FirstNames = new[]
                                                  {
                                                      "Abby", "Beth", "Carol", "Deborah", "Eve", "Farrah", "Gail", "Hope",
                                                      "Iris", "Judy", "Karen", "Lisbeth", "Mary", "Nancy", "Orchard",
                                                      "Patty", "Sarah", "Tricia", "Valerie"
                                                  };

        public static string[] FirstNames2 = new[]
                                                  {
                                                      "Apple", "Becky", "Connie", "Dina", "Eddie", "Fig", "Gabby", "Hannah",
                                                      "Ida", "Jen", "Kathy", "Laura", "Maven", "Nicky", "Orchid",
                                                      "Pine", "Samantha", "Tina", "Vicky"
                                                  };

        public static HashSet<Skater> CreateHomeTeamSkaters(string lastName)
        {
            return CreateTestSkaters(lastName, FirstNames,"H", 1);
        }

        public static HashSet<Skater> CreateAwayTeamSkaters(string lastName)
        {
            return CreateTestSkaters(lastName, FirstNames2, "A", FirstNames2.Length + 1);
        }

        public static HashSet<Official> CreateTestOfficials()
        {
            var testOfficials = new HashSet<Official>();
            foreach (var name in FirstNames.Union(FirstNames2))
            {
                var newOfficial = new Official(
                        string.Format("{0} Official", name), 
                        string.Format("O{0}",testOfficials.Count+1));
                testOfficials.Add(newOfficial);
            }
            return testOfficials;
        }

        private static HashSet<Skater> CreateTestSkaters(string lastName, string[] firstNames, string numberPrefix, int startIdAt)
        {
            var testSkaters = new HashSet<Skater>();
            foreach (var name in firstNames)
            {
                var skater = new Skater(
                    string.Format("{0} {1}", name, lastName),
                    string.Format("{0}{1}", numberPrefix, testSkaters.Count + 1));
                testSkaters.Add(skater);
            }
            return testSkaters;
        }

        public static Roster CreateTestRoster(Team team, string color)
        {
            var testSkaters = team.Skaters;
            var testRoster = new Roster(team.Name, color);
            testRoster.AddCaptain(testSkaters.First());
            foreach (var skater in testSkaters.Skip(1).Take(13))
            {
                testRoster.AddSkater(skater);
            }

            return testRoster;
        }

        public static Lineup CreateLineupFromRosterForJam(Roster roster,Jam jam)
        {
            Contract.Requires(roster != null);
            Contract.Requires(roster.Count() >= 5);
            return new Lineup(jam, roster.First(), roster.Skip(1).First(), new HashSet<Player>(roster.Skip(2).Take(3)));
        }

        public static Lineup CreateOtherLineupFromRosterForJam(Roster roster, Jam jam)
        {
            Contract.Requires(roster != null);
            Contract.Requires(roster.Count() >= 5);
            return new Lineup(jam, roster.First(), roster.Skip(3).First(), new HashSet<Player>(roster.Skip(4).Take(3)));
        }

        public static Match CreateTestMatch(DbMatchRepository repository, bool isRegulation, bool isSanctioned)
        {
            return CreateTestMatch(repository, isRegulation, isSanctioned, "Test Match");
        }

        public static Match CreateTestMatch(DbMatchRepository repository, bool isRegulation, bool isSanctioned, string matchName)
        {
            var homeTeam = new Team("Home") { Skaters = CreateHomeTeamSkaters("Home") };
            var awayTeam = new Team("Away") { Skaters = CreateAwayTeamSkaters("Away") };

            var homeRoster = CreateTestRoster(homeTeam, "White");
            var awayRoster = CreateTestRoster(awayTeam, "Black");

            var testMatch = repository.Create(
                isRegulation,
                isSanctioned,
                homeRoster,
                awayRoster,
                matchName,
                "Nowhere",
                DateTime.Now);
           
            return testMatch;
        }
    }
}
